package com.jshan.blog.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.jshan.blog.annotation.OperationLog;
import com.jshan.blog.enums.BizCodeEnums;
import com.jshan.blog.result.Result;
import com.jshan.blog.service.FileUploadService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;

@RestController
@RequestMapping("/file")
@Slf4j
@RefreshScope
public class UploadFileController {

    @Resource
    private FileUploadService fileUploadService;

    @Value("${storage.service.type}")
    private String storageType;

    @PostMapping
    @OperationLog(value = "上传图片以及文件", operationType = "新增")
    // @SaIgnore
    public Result uploadImage(@RequestParam("file") MultipartFile file, @RequestParam("type") String type) {
        try {
            HashMap<String, String> map = new HashMap<>();
            String url = fileUploadService.uploadFile(file, type, storageType);
            if (StringUtils.isNotBlank(url)) {
                map.put("url", url);
                return Result.ok(map);
            }
            return Result.fail(BizCodeEnums.FAILE.getCode(), "上传失败");
        } catch (Exception e) {
            log.error("错误信息:{}", e.getMessage(), e);
            return Result.fail(BizCodeEnums.FAILE.getCode(), "上传失败");
        }
    }


}
